Page({  
  data: {
    globalData: getApp().globalData,
    add: true,
    addressId: '',
    name: '',
    phone: '',
    area: '',
    detail: ''
  },

  onLoad(options) {
    if(options.id != 'add') {
      this.getAddress(options.id)
      this.setData({add: false})
    }
  },

  getAddress(id) {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/address/' + id,
      method: 'GET',
      success(res) {
        let address = res.data.data
        that.setData({
          addressId: address.addressId,
          name: address.name,
          phone: address.phone,
          area: address.area,
          detail: address.detail
        })
      }
    })
  },
  submit() {
    let that = this
    wx.request({
      url: that.data.globalData.server + '/address',
      method: that.data.add ? 'POST' : 'PUT',
      data: {
        addressId: that.data.addressId,
        userId: wx.getStorageSync('user').userId,
        name: that.data.name,
        phone: that.data.phone,
        area: that.data.area,
        detail: that.data.detail,
      },
      success(res) {
        wx.navigateTo({url: '/pages/address/address',})
      }
    })
  }
})